import { createRouter, createWebHistory } from 'vue-router'
import Layout from '@/views/layout/index.vue'
import Home from '@/views/home/index.vue'
import NotFound from '@/views/404/index.vue'

let routes = [
  {
    path: '/',
    component: Layout,
    children: [
      {
        path: '',
        component: Home,
        meta: { title: '首页 Life & Cycle' }
      }, {
        path: 'article/:category/:id',
        component: () => import('@/views/article/index.vue'),
      }, {
        path: 'article/:category',
        component: () => import('@/views/category/index.vue'),
        meta: { title: '文章分类' }
      }
    ]
  }, {
    path: '/news',
    component: () => import('@/views/news/index.vue'),
    meta: { title: '新闻列表' }
  }, {
    path: '/about',
    component: () => import('@/views/about/index.vue'),
    meta: { title: '关于本站' }
  }, {
    path: '/essay/:id',
    component: () => import('@/views/essay/index.vue'),
    meta: { title: '随笔' }
  }
  // , {
  //   path: '/guestbook',
  //   component: () => import('@/views/guest/index.vue'),
  //   meta: { title: '留言板~' }
  // }
  , {
    path: '/feedback',
    component: () => import('@/views/feedback/index.vue'),
    meta: { title: '反馈建议', }
  }, {
    path: '/paint',
    component: () => import('@/views/paint/index.vue'),
    meta: { title: '画廊' }
  },
  {
    path: '/test',
    component: () => import('@/views/book/index.vue'),
  }, 
  {
    path: '/404',
    component: NotFound,
    meta: { title: '404' }
  }, {
    path: '/:catchAll(.*)',
    redirect: '/404'
  }
]

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes,
  scrollBehavior: () => ({ top: 0, left: 0, behavior: 'instant' }),
})

import { loadingStart, loadingDone } from '@/components/loadingBar'

router.beforeEach((to, from, next) => {
  if (to.meta.title) document.title = to.meta.title

  if (to.fullPath !== from.fullPath) loadingStart()
  next()
})

router.afterEach((to, from) => {
  if (to.fullPath !== from.fullPath) loadingDone()
})


export default router